package org.example.javaee.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.example.javaee.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller  //相当于LoginController loginController = new LoginController();
@RequestMapping("/Login")
public class LoginController {
    private static int visitCount = 0;
    @RequestMapping("getPage")
    public String getPage(String page, Model model) {
        if ("Body".equals(page)) {
            synchronized (LoginController.class) {
                visitCount++;
            }
        }
        model.addAttribute("visitCount", visitCount);
        return page;   //跳转到page页面
    }
    @RequestMapping("validate")
    public String validate(Student student, HttpServletRequest request, Model model) {
        synchronized (LoginController.class) {
            visitCount++;
        }
        //在控制台打印名字和密码
        System.out.println(request.getRemoteAddr()+",your name is " +student.getName()+",your password is " +student.getPassword());
        //将username和password变量传出到Body页面
        model.addAttribute("student", student);
        model.addAttribute("username", student.getName());
        model.addAttribute("visitCount", visitCount);
        return "Body"; //跳转到Body.html页面
    }
}
// http://localhost:8080/...(输入静态网页的地址名加类型，如/index.html)
// http://10.10.74.46:8081/Login/getPage?page=....(要输出的动态地址名)

